﻿#include"MainWidget.h"

extern "C"
{
        #include <fcntl.h>
        #include <sys/stat.h>
        #include <semaphore.h>
        #include <sys/types.h>
        #include <sys/ipc.h>
        #include <sys/shm.h>

        #include "bitstream.h"
        #include "config.h"
        #include "mask.h"
        #include "mmask.h"
        #include "mqrspec.h"
        #include "qrencode.h"
        #include "qrinput.h"
        #include "qrspec.h"
        #include "rscode.h"
        #include "split.h"

}


QStackedLayout* stackLayout;
chargpile_data* chargingData;
KeyBoard *dlg;
MyThread mt;//刷卡线程
RefreshGun Refreash_Thread[GUN_AMOUNT]; // gun refresh
int focus_gun_;

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setFixedSize(800, 480);
    focus_gun_ = 0;//初始化状态为没有焦点枪
    pixmapsignal = new QPixmap;
    dlg = new KeyBoard(this);
    onCreateSharedMemory();//创建共享内存
    onCreateUi();
    SignalsConnect();
    checkShareMemory = 0;
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        for(int j=0; j<9; j++)
        {
            GUN_STATE[i].CountDownisActivity[j] = 60;
            GUN_STATE[i].VINflag = 0;
        }
        Refreash_Thread[i].GUN_NO = i;//0~7初始化各线程控制的枪
        Refreash_Thread[i].start();
        Refreash_Thread[i].ErrorTimer->start(300);
        Refreash_Thread[i].swipcard_YCT = 0;
    }

//    onCreateModel();//创建模型
}

void MainWidget::onCreateSharedMemory()//创建共享内存
{
    int shmid = -1;
    key_t uikey;
    sem_t* ui2m_sem;
    sem_t* m2ui_sem;
    ui2m_sem = sem_open("uitomain", O_CREAT, 0644, 0);
    m2ui_sem = sem_open("maintoui", O_CREAT, 0644, 0);
    sem_wait(m2ui_sem);
    uikey = ftok("/etc/hostname", 2);
    shmid = shmget(uikey, sizeof(chargpile_data), IPC_CREAT | SHM_R | SHM_W);
//    shmid = shmget(0x5A5A5A5A, sizeof(chargpile_data), IPC_CREAT | SHM_R | SHM_W);


    if( shmid == -1 )
    {
        sem_close(ui2m_sem);
        sem_close(m2ui_sem);
        sem_unlink("uitomain");
        sem_unlink("maintoui");
    }

    chargingData = reinterpret_cast<chargpile_data*>(shmat(shmid, NULL, 0));
    if( chargingData == reinterpret_cast<void*>(-1) )
    {
        sem_close(ui2m_sem);
        sem_close(m2ui_sem);
        sem_unlink("uitomain");
        sem_unlink("maintoui");
    }
    printf("UI共享内存的key:0X%X ID:%d 大小: %d; 地址: %p\n",uikey ,shmid,sizeof(chargpile_data), chargingData);
}

void MainWidget::onCreateUi()
{
    stackLayout = new QStackedLayout(this);
    UseWait = new Widget(this);
    SystemSetting1 = new SystemSetting_1(this);
    SystemSetting2 = new Systemsetting_2(this);
    SystemSetting3 = new SystemSetting_3(this);
    SystemSetting4 = new SystemSetting_4(this);
    ChargeCardManage = new ChargeCardManageInfo(this);
    RecordofCharge = new RecordOfCharge(this);
    FilterRecordofCharge = new FilterRecordOfCharge(this);
    WaringInfomation = new WaringInfo(this);
    ExportRecordofCharge = new ExportRecordOfCharge(this);
    Carvertify = new CarVertify(this);
    Chargetype = new ChargeType(this);
    SettingofChargeMoney = new SettingOfChargeMoney(this);
    SettingofChargePower = new SettingOfChargePower(this);
    SettingofChargeTime = new SettingOfChargeTime(this);
    Maintainloging = new MaintainLoging(this);
    Waringinfomation = new WarnningDetailInfo(this);
    Chargesubscribe = new ChargeSubscribe(this);
    ChargewatchingDC = new ChargeWatchingDC(this);
    ChargewatchingAC = new ChargeWatchingAC(this);
    Settleaccount = new SettleAccount(this);
    ReturnchargeGun = new RetrunChargeGun(this);
    EndofChargeVerify = new EndOfChargeVerify(this);
    ChargeGunnotConnect = new ChargeGunNotConnect(this);
    ChargeGunnotnotConnect = new ChargeGunNotNotConnect(this);
    CarAndqRCodeVerify = new CarAndQRCodeVerify(this);
    Equipmentmaintaince = new EquipmentMaintaince(this);
    Equipmentupgrade = new EquipmentUpgrade(this);
    WaitSwipcard = new WaitSwipCard(this);
    Networkinfo = new NetworkInfo(this);
    Choosegun = new ChooseGun(this);
    OutstandingOrder = new Outstanding_Order(this);
    Guninfosetting = new GuninfoSetting(this);
    Changeguninfo = new ChangeGuninfo(this);
    Setrate = new SetRate(this);

    stackLayout->addWidget(UseWait);//page0
    stackLayout->addWidget(SystemSetting1);//系统设置1的界面page1
    stackLayout->addWidget(SystemSetting2);//系统设置2的界面page2
    stackLayout->addWidget(SystemSetting3);//系统设置3的界面page3
    stackLayout->addWidget(SystemSetting4);//系统设置4的界面page4
    stackLayout->addWidget(ChargeCardManage);//充电卡管理信息界面page5
    stackLayout->addWidget(RecordofCharge);//充电记录浏览page6
    stackLayout->addWidget(FilterRecordofCharge);//充电记录筛选page7
    stackLayout->addWidget(WaringInfomation);//告警信息查询;page8
    stackLayout->addWidget(ExportRecordofCharge);//导出充电记录page9
    stackLayout->addWidget(Carvertify);//请输入卡号和密码page10
    stackLayout->addWidget(Chargetype);//充电类型page11
    stackLayout->addWidget(SettingofChargeMoney);//充电金额设置page12
    stackLayout->addWidget(SettingofChargePower);//充电电量设置page13
    stackLayout->addWidget(SettingofChargeTime);//充电时长设置page14
    stackLayout->addWidget(Maintainloging);//设置维护登录界面page15
    stackLayout->addWidget(Waringinfomation);//详细告警内容界面page16
    stackLayout->addWidget(Chargesubscribe);//预约界面page17
    stackLayout->addWidget(ChargewatchingDC);//直流计费界面;page18
    stackLayout->addWidget(ChargewatchingAC);//交流计费界面;page19
    stackLayout->addWidget(Settleaccount);//结算界面;page20
    stackLayout->addWidget(ReturnchargeGun);//归还抢界面page21
    stackLayout->addWidget(EndofChargeVerify);//输入验证码界面page22
    stackLayout->addWidget(ChargeGunnotConnect);//充电枪未连接界面page23
    stackLayout->addWidget(ChargeGunnotnotConnect);//充电枪未可靠连接界面page24
    stackLayout->addWidget(CarAndqRCodeVerify);//刷卡扫码界面page25
    stackLayout->addWidget(Equipmentmaintaince);//维护界面page26
    stackLayout->addWidget(Equipmentupgrade);//升级界面page27
    stackLayout->addWidget(WaitSwipcard);//请刷充电管理卡page28
    stackLayout->addWidget(Networkinfo);//网络详情page29
    stackLayout->addWidget(Choosegun);//选择枪号page30
    stackLayout->addWidget(OutstandingOrder);//未完成历史订单page31
    stackLayout->addWidget(Guninfosetting);//32
    stackLayout->addWidget(Changeguninfo);//33
    stackLayout->addWidget(Setrate);//34

    setLayout(stackLayout);

    stackLayout->setCurrentIndex(ChooseGunPage);
}

void MainWidget::SignalsConnect()
{
    connect(&heart, SIGNAL(timeout()), this, SLOT(heartFunction()));
    heart.start(3000);//30s后检测共享内存数据
    //设备升级;
    connect(&timerUpdateDevice, SIGNAL(timeout()), this, SLOT(onUpdateDevice()));
    timerUpdateDevice.start(1000);

//    connect(&DebugTimer, SIGNAL(timeout()), this, SLOT(qDebugInfo()));
//    DebugTimer.start(1500);
    DebugTimer.start();

    connect(&UpdateDate, SIGNAL(timeout()), this, SLOT(onUpdateTime()));
    UpdateDate.start(200);//更新时间的定时器，0.2秒发送一次onUpdateDateTime()信号，从未停止过
    connect(&UpdateSignal, SIGNAL(timeout()), this, SLOT(onUpdateSignal()));
    UpdateSignal.start(5000);//更新信号强度的定时器

/***************充电管理卡界面与请刷管理卡的界面之间的通讯建立****************/
    connect(SystemSetting4, SIGNAL(RequestIntoChargeManageCardPage()), ChargeCardManage, SLOT(AdministrationCard_Binded()));
    connect(ChargeCardManage, SIGNAL(SwipingAdministrationCard(int, QString, QString)),WaitSwipcard, SLOT(ReferAdministerCardMod(int, QString, QString)));
    connect(ChargeCardManage, SIGNAL(BindingCard(int, QLineEdit *,QString, QString )), WaitSwipcard, SLOT(BindCardMode(int, QLineEdit *,QString, QString)));
    connect(ChargeCardManage, SIGNAL(UnBindingCard(int,QString,QString,QString)), WaitSwipcard, SLOT(UnBindCardMode(int, QString, QString, QString)));
    connect(WaitSwipcard, SIGNAL(BindisOk(int)), ChargeCardManage, SLOT(LabelShow(int)));
    connect(WaitSwipcard, SIGNAL(UnBindingSignal(int)), ChargeCardManage, SLOT(LabelShow(int)));

/***************筛选充电记录与充电记录界面之间的通讯建立*********************/
    connect(FilterRecordofCharge, SIGNAL(onFiltrateData(int, QString, QString, QString)), RecordofCharge, SLOT(FitrateDataShow(int, QString, QString, QString)));

/***************充电故障信息与充电故障详细信息界面的通讯建立*****************/
    connect(WaringInfomation, SIGNAL(Check_Alert_Details(int)), Waringinfomation, SLOT(ShowStopReason(int)));

/***************选择枪号界面与后台主界面的通讯建立***********************/
    connect(Choosegun, SIGNAL(ChooseGun_No(int)), this, SLOT(Gun_Showing_timeout()));

    connect(&Gun_Showing, SIGNAL(timeout()), this, SLOT(takeout_guninfo()));

/***************刷卡或扫码界面与充电管理卡信息界面的通讯建立,为获取充电管理卡的信息***********************/
    connect(CarAndqRCodeVerify, SIGNAL(get_Manage_CardNum(QString*)), ChargeCardManage, SLOT(SendManagementtoCardAndQRcord(QString*)));

/***************刷卡或扫码界面与刷新枪结构信息线程的通讯建立*****************/
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        connect(CarAndqRCodeVerify, SIGNAL(_25Page_Swip(QString, int)), Refreash_Thread+i, SLOT(ReceiveSwipsignal(QString , int)));

/**************选择自动充满时直接通知线程开始进行充电监控*********************/
        connect(Chargetype, SIGNAL(StartCharging_RefreshChargeInfo(int)), Refreash_Thread+i, SLOT(ChargingInfomation_Monitoring(int)));
/**************选择按金额充电时发出信号让线程开始充电监控**********************/
        connect(SettingofChargeMoney, SIGNAL(StartCharging_RefreshChargeInfo(int)), Refreash_Thread+i, SLOT(ChargingInfomation_Monitoring(int)));
/**************选择按电量充电时*************************/
        connect(SettingofChargePower, SIGNAL(StartCharging_RefreshChargeInfo(int)), Refreash_Thread+i, SLOT(ChargingInfomation_Monitoring(int)));
/**************选择按时间充电***************************/
        connect(SettingofChargeTime, SIGNAL(StartCharging_RefreshChargeInfo(int)), Refreash_Thread+i, SLOT(ChargingInfomation_Monitoring(int)));
        /*****app充电信号**********************/
        connect(CarAndqRCodeVerify, SIGNAL(appChargingDataUpdate(int)), Refreash_Thread+i, SLOT(ChargingInfomation_Monitoring(int)));
    }
/***************充电监控界面与结算订单界面*******************/
    connect(ChargewatchingDC, SIGNAL(Settle_Accounts(int)), Settleaccount, SLOT(updateSettleAccount(int)));
/***************充电结算界面与归还充电请界面通讯建立*******************/
    connect(Settleaccount, SIGNAL(returnChargeGunCountdownStart(int)), ReturnchargeGun, SLOT(returnChargeGunCountdownStartSlot(int)));

    /*************系统设置选枪与修改枪信息的通讯建立********************/
    connect(Guninfosetting, SIGNAL(SetGuninfo(int)), Changeguninfo,SLOT(TitleChange(int)));

    connect(this, SIGNAL(On_line()), ChargewatchingDC, SLOT(On_lineSlot()));
    connect(this, SIGNAL(Off_line()), ChargewatchingDC, SLOT(Off_lineSlot()));

}

MainWidget::~MainWidget()
{
    delete stackLayout;
    free(chargingData);
    free(card);
    //free(Refreash_Thread);
}
